var UserTable = {

	table: null,
	getedChildren: [],
	openedChildren: {},
	allRows: {},

	init: function(params)
	{
		UserTable.table = document.getElementById(params.tId);
	},

	_getRowByUserId:function(user_id)
	{
		return UserTable.allRows[Number(user_id)];
	},

	getChildren:function(user_id)
	{
		if(!UserTable.table) return;
		openLoading();
		var parentRow = UserTable._getRowByUserId(user_id);
		var l = parentRow.getAttribute('level') ? parentRow.getAttribute('level') : 0;
		l++;
		$.ajax({
			url: base_url + 'ajax/get_my_user_children/' + user_id + "/" + l,
			type: 'GET',
			data: {},
			success: function(data)
			{	
				UserTable.addRowsByJson(eval("(" + data + ")"), user_id);
				closeLoading();
			}
		});
	},

	addRowsByJson:function(obj, parent_id)
	{
		UserTable.getedChildren.push(parent_id);
		UserTable.openedChildren[parent_id] = true;
		if(!UserTable.table) return;
		var parentRow = UserTable._getRowByUserId(parent_id);
		var num = 0;
		var lastRow = parentRow;
		var l = parentRow ? Number(parentRow.getAttribute('level')) + 1 : 0;
		for(var i in obj)
		{
			var u = obj[i];
			num++;
			var row = UserTable.createRowByObject(u, num);
			row.setAttribute('level', l);
			parentRow ? DOM.insertAfter(UserTable.table, row, lastRow) : UserTable.table.appendChild(row);
			UserTable.allRows[Number(u.id)] = row;
			lastRow = row;
		}
	},

	createRowByObject:function(user, num)
	{
		var row = document.createElement("TR");
		row.setAttribute('parent_id', user.parent_id);
		row.setAttribute('user_id', user.id);
		row.onmouseover = function() {
			if(this.className != "clicked" && this.className != "selected") this.className = "hover";
		}
		row.onmouseout = function() {
			if(this.className != "clicked" && this.className != "selected") this.className = "";
		}

		var cell1 = row.insertCell(0);
		cell1.innerHTML = '<a name="' + user.id + '"></a> <a href="oper/?t[]=reg&t[]=buy&t[]=ng&t[]=ref&t[]=week&t[]=sell&t[]=modify&u[]=' + user.id + '&inc_des[]=' + user.id + '" title="Операции"><img src="' + image_path + 'images/icons/table.png"></a>';

		var cell2 = row.insertCell(1);
		cell2.onclick = UserTable.cellClick(cell2);
		cell2.style.paddingLeft = user.otstup + "px";
		cell2.appendChild(document.createTextNode(num + ". "));
		var statusTextNode = document.createTextNode(user.status);
		if(user.level != '')
		{
			var a = document.createElement("A");
			a.href = user.id;
			a.appendChild(statusTextNode);
			a.onclick = UserTable.userClick(user.id);
			cell2.appendChild(a);
		}
		else
		{
			cell2.appendChild(statusTextNode);
		}

		var cell3 = row.insertCell(2);
		cell3.onclick = UserTable.cellClick(cell3);
		cell3.innerHTML = "<b>" + user.fio + "</b><br>" + user.email;

		var cell4 = row.insertCell(3);
		cell4.onclick = UserTable.cellClick(cell4);
		cell4.innerHTML = user.date;

		var cell5 = row.insertCell(4);
		cell5.onclick = UserTable.cellClick(cell5);
		cell5.innerHTML = user.otkuda;

		var cell6 = row.insertCell(5);
		cell6.onclick = UserTable.cellClick(cell6);
		cell6.innerHTML = (user.skype ? "Skype: " + user.skype + "<br>" : "") + (user.icq ? "ICQ: " + user.icq + "<br>" : "") + (user.phone ? "Телефон: " + user.phone : "");

		var cell7 = row.insertCell(6);
		cell7.onclick = UserTable.cellClick(cell7);
		cell7.innerHTML = (user.ref_fio ? user.ref_fio + "<br>" : "") + (user.ref_email ? user.ref_email : "");

		var cell8 = row.insertCell(7);
		cell8.innerHTML = '<a href="user/info/' + user.id + '" title="Полное досье"><img src="' + image_path + 'images/icons/application_list.png"></a> &nbsp; <a href="user/edit/' + user.id + '" title="Редактировать"><img src="' + image_path + 'images/icons/edit.gif"></a>' + (user.can_delete ? ' &nbsp; <a href="user/delete/' + user.id + '" title="Удалить"><img src="' + image_path + 'images/icons/delete.gif"></a>' : '');

		return row;
	},

	closeChildren:function(pId)
	{
		if(!UserTable.table) return;
		pId = String(pId);
		for(var i = 0; i < UserTable.table.rows.length; i++)
		{
			if(pId == UserTable.table.rows[i].getAttribute('parent_id'))
			{
				UserTable.table.rows[i].style.display = 'none';
				UserTable.closeChildren(UserTable.table.rows[i].getAttribute('user_id'));
			}
		}
		UserTable.openedChildren[Number(pId)] = false;
	},

	openChildren:function(pId)
	{
		if(!UserTable.table) return;
		pId = String(pId);
		for(var i = 0; i < UserTable.table.rows.length; i++)
		{
			if(pId == UserTable.table.rows[i].getAttribute('parent_id'))
			{
				UserTable.table.rows[i].style.display = '';
			}
		}
		UserTable.openedChildren[Number(pId)] = true;
	},

	userClick:function(user_id)
	{
		return function()
		{
			return UserTable.openChildern(user_id);
		}
	},

	openChildern: function(user_id)
	{
		user_id = Number(user_id);
		if(inArray(UserTable.getedChildren, user_id))
		{
			if(UserTable.openedChildren[user_id])
			{
				UserTable.closeChildren(user_id);
			}
			else
			{
				UserTable.openChildren(user_id);
			}
		}
		else
		{
			UserTable.getChildren(user_id);
		}
		return false;
	},

	cellClick: function(cell)
	{
		return function()
		{
			if(cell.parentNode.className != 'selected') 
			{
				cell.parentNode.className = cell.parentNode.className == 'clicked' ? '' : 'clicked';
			}
		}
	},

	selectRows: function(ids)
	{
		for(var i in ids)
		{
			var row = UserTable._getRowByUserId(ids[i]);
			if(row)
			{
				row.className = "selected";
			}
		}
	},

	goToFirstSelected: function()
	{
		for(var i = 0; i < UserTable.table.rows.length; i++)
		{
			if(UserTable.table.rows[i].className == 'selected')
			{
				var prev_user_id = UserTable.table.rows[i - 2].getAttribute('user_id');
				if(prev_user_id)
				{
					document.location.href = document.location.href + "#" + prev_user_id;
					break;
				}
			}
		}
	}
};